//
//  GQJFPSLabel.m
//  TotalDemo
//
//  Created by gujian on 16/8/18.
//  Copyright © 2016年 gujian. All rights reserved.
//

#import "GQJFPSLabel.h"

@implementation GQJFPSLabel{

    CADisplayLink *_link;
    NSUInteger _count;
    NSTimeInterval _lastTime;

}

- (instancetype)initWithFrame:(CGRect)frame
{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.font = [UIFont systemFontOfSize:15.0];
        self.textColor = [UIColor redColor];
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.7];
        _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(tick:)];
        [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    }
    
    return self;
}

- (void)dealloc
{
    [_link invalidate];
}

- (void)tick:(CADisplayLink *)link
{
    if (_lastTime == 0) {
        _lastTime = link.timestamp;
        return;
    }
    
    _count++;
    NSTimeInterval delta = link.timestamp - _lastTime;
    if (delta < 1) {
        return;
    }
    
    _lastTime = link.timestamp;
    float fps = _count / delta;
    _count = 0;
    
    NSString *str = [NSString stringWithFormat:@"%d FPS",(int)round(fps)];
    self.text = str;
    
    
}



@end
















